package com.neusoft.nep.PublicSupervisor.controller;
import com.neusoft.nep.common.AlertUtils;
import com.neusoft.nep.common.FileUtils;
import com.neusoft.nep.entity.Feedback;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

public class SelectaqiController {
    @FXML
    private ToggleGroup selectgroup;
    @FXML
    private TextArea txtDescription;
    @FXML
    private Button submit;

    public void submit() throws IOException {
        RadioButton selectedRadio = (RadioButton) selectgroup.getSelectedToggle();
        String levelText = selectedRadio == null ? "" : selectedRadio.getText();
        String description = txtDescription.getText();

        if (levelText.isEmpty() || description.isEmpty()) {
            AlertUtils.showDialog("提交错误", "请选择AQI等级并填写空气质量描述");
            return;
        }


        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String currentTime = now.format(formatter);

        // 生成反馈记录
        Feedback feedback = new Feedback();
        feedback.setTime(currentTime);
        feedback.setProvince(SelectgridController.currentProvince);
        feedback.setCity(SelectgridController.currentCity);
        feedback.setAddress(SelectgridController.currentAddress);
        feedback.setAqiLevel(((RadioButton)selectgroup.getSelectedToggle()).getText());
        feedback.setDescription(description);
        feedback.setIndex(getNextIndex());  // 设置唯一索引
        // 保存当前登录用户的姓名
        feedback.setName(LoginController.currentUserName);
        // 保存当前登录用户的生日
        feedback.setBirthday(LoginController.currentBirthday);
        // 保存当前登录用户的性别
        feedback.setSex(LoginController.currentSex);
        // 保存当前登录用户的手机号
        feedback.setPhoneNumber(LoginController.currentPhoneNumber);
        // 获取所选AQI按钮的颜色
        String aqiColor = selectedRadio.getStyle().replace("-fx-background-color: ", "");
        feedback.setAqiColor(aqiColor);
        // 保存反馈记录
        saveFeedback(feedback);

        AlertUtils.showDialog("提交成功", "空气质量监督信息已提交");
        goToFeedbackList();
    }

    private int getNextIndex() {
        Map<String, Feedback> feedbackMap = FileUtils.readMapObject("feedback.txt");
        //生成下一个可用的反馈记录索引
        return feedbackMap.isEmpty() ? 1 : feedbackMap.size() + 1;
    }

    private void saveFeedback(Feedback feedback) {
        Map<String, Feedback> feedbackMap = FileUtils.readMapObject("feedback.txt");
        feedbackMap.put(String.valueOf(feedback.getIndex()), feedback);
        FileUtils.writeMapObject("feedback.txt", feedbackMap);
    }
    public void goToFeedbackList() throws IOException {
        //获取Stage
        Stage stage = (Stage) submit.getScene().getWindow();
        //加载注册页面
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/Feedbacklist.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = submit.getScene();
        scene.setRoot(root);
        stage.setScene(scene);
        stage.show();
    }
}